<script setup>
import pageContainer from '@/components/pageContainer.vue'
import { ref } from 'vue'

import { userUpdatePasswordService } from '@/api/user'
import { useUserStore } from '@/stores'
import router from '@/router'

const  pwdRef = ref()

const pwdForm = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})

const checkDifferent = ( rule, value, callback ) =>{
  // 校验新密码和原始密码不能一样
  if( value === pwdForm.value.old_pwd ){
    callback( new Error('新密码不能与原密码一样') )
  }else{
    callback()
  }
}

const checkSameAsNewPwd = ( rule, value, callback ) =>{
  // 校验确认密码必须和新密码一样
  if(value !== pwdForm.value.new_pwd){
    callback(new Error('确认密码必须和新密码一样'))
  }else{
    callback()
  }
}

const rules = ref({
  old_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '原密码长度在6-15位之间', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '原密码长度在6-15位之间', trigger: 'blur' },
    { validator: checkDifferent, trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '原密码长度在6-15位之间', trigger: 'blur' },
    { validator: checkSameAsNewPwd, trigger: 'blur' }
  ]
})

/**
 * 修改密码
 */
const userStore = useUserStore()
// const router = useRouter()
const submitFrom = async() =>{
  await pwdRef.value.validate()
  await userUpdatePasswordService(pwdForm.value)
  ElMessage.success('修改密码成功！')

  // 密码修改成功后，退出登录，重新登录
  // 清空本地存储 token 和 个人信息
  userStore.setToken('')
  userStore.setUser({})
  
  // 拦截到登录页
  router.push('/login') 
}

/**
 * 重置密码
 */
const restForm = () =>{
  pwdRef.value.resetFields()
}

</script>

<template>
  <page-container title="重置密码">
    <el-row>
      <el-col :span="12">
        <el-form ref="pwdRef" :model="pwdForm" :rules="rules" label-width="100px">
          <el-form-item label="原始密码" prop="old_pwd">
            <el-input v-model="pwdForm.old_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input v-model="pwdForm.new_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="re_pwd">
            <el-input v-model="pwdForm.re_pwd" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitFrom">修改密码</el-button>
            <el-button @click="restForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </page-container>
</template>